package com.zxy.controller;

import com.zxy.model.MusicInfo;
import com.zxy.service.AdminService;
import com.zxy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Controller
public class AdminController {
    
    @Resource
    private AdminService adminService;
    @Resource
    private MusicInfo musicInfo;
    @Resource
    private UserService userService;
    
    @RequestMapping("deleteMusic")
    public void deleteMusic(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String musicName = request.getParameter("musicName");
        String path = request.getServletContext().getRealPath("music");
    
        //通过文件路径找到文件，然后删除文件
        File musicFile = new File(path +"/"+ musicName.split("/")[1]);
        System.out.println("delete_path:" + path + musicName.split("/")[1]);
        boolean b_file = false;
        if(musicFile.exists()){
            b_file = musicFile.delete();
        }else{
            System.out.println("歌曲不存在");
        }
        musicName = musicName.split("/")[1].split(".mp3")[0];
        boolean b_sql = adminService.deleteMusic(musicName);
    
        if(b_sql && b_file){
            //更新session中歌曲数据
            request.getSession().removeAttribute("musicList");
            request.getSession().setAttribute("musicList",userService.getAllMusic());
            request.getSession().setAttribute("deleteSuccess", "已经删除歌曲！");
            response.sendRedirect("musicList");
        }else{
            response.sendRedirect("musicList");
        }
    }
    
    @RequestMapping("updateDB")
    public void updateDB( HttpServletRequest request, HttpServletResponse response) throws IOException {
        adminService.updateMusic(request.getServletContext().getRealPath("music"));
        request.getSession().removeAttribute("musicList");
        request.getSession().setAttribute("musicList",userService.getAllMusic());
        String json="{\"result\":\""+true+"\",\"msg\":\""+"ok"+"\"}";
        response.getWriter().write(json);
    }
    
    @RequestMapping("uploadMusic")
    public  void uploadMusic(@RequestBody @RequestParam("file1") CommonsMultipartFile[] commonsMultipartFile,
                             HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String msg = "";
        try {
            for (CommonsMultipartFile items : commonsMultipartFile) {
                if (items.getSize()>0){
                    String filename = items.getOriginalFilename();
                    if (filename.endsWith(".mp3")){
                        musicInfo.setMusicName(filename.substring(0,filename.length()-4));
                        if (!adminService.musicIsExist(musicInfo)){
                            String fullpath=request.getServletContext().getRealPath("music")+"/"+filename;
                            musicInfo.setMusicPath("music/"+filename);
                            File file = new File(fullpath);
                            items.transferTo(file);
                            adminService.insert(musicInfo);
                            request.getSession().removeAttribute("musicList");
                            request.getSession().setAttribute("musicList",userService.getAllMusic());
                            msg="{" +
                                    "  \"result\":\"true\"," +
                                    "  \"msg\":\"上传成功\"" +
                                    "}";
                        
                        }else{
                            msg="{\"result\":\"false\",\"msg\":\"你上传的文件已存在！\"}";
                        }
                    }else{
                        msg="{\"result\":\"false\",\"msg\":\"你上传的文件不合法！\"}";
                    }
                }else {
                    msg="{\"result\":\"false\",\"msg\":\"请选择文件！\"}";
                }
            
            }
        
            response.getWriter().write(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
